package entity;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * This is BoardMemento entity.
 * Store the status of Board. 
 */
public class BoardMemento implements Serializable{
	private static final long serialVersionUID = -3442140653653557324L;
	
	private ArrayList<Word> unprotectedWords;
	private ArrayList<Word> protectedWords;
	private ArrayList<Poem> poems;
	
	/**
	 * Constructor of BoardMemento.
	 * Get words, poems from board. 
	 */
	public BoardMemento(Board board){
		ArrayList<Word> unprotectedClones = new ArrayList<Word>();
		ArrayList<Word> protectedClones = new ArrayList<Word>();
		ArrayList<Poem> poemClones = new ArrayList<Poem>();
		
		for(Word w : board.getUnprotectedWords()){
			unprotectedClones.add(new Word(w));
		}
		
		for(Word w : board.getProtectedWords()){
			protectedClones.add(new Word(w));
		}
		
		for(Poem p : board.getPoems()){
			poemClones.add(new Poem(p));
		}

		unprotectedWords = unprotectedClones;
		protectedWords = protectedClones;
		poems = poemClones;
	}
	
	/**
	 * Replace duplicates.
	 */
	public void replaceDuplicates(){
		for(Poem p : poems){
			for(Row r : p.getRows()){
				for(Word w : r.getWords()){
					if(protectedWords.contains(w)){
						protectedWords.remove(w);
						protectedWords.add(w);
					}
				}
			}
		}
	}

	/**
	 * Get unprotected words.
	 * @return
	 */
	public ArrayList<Word> getUnprotectedWords() {
		return unprotectedWords;
	}

	/**
	 * Get protected words.
	 * @return
	 */
	public ArrayList<Word> getProtectedWords() {
		return protectedWords;
	}

	/**
	 * Get poems. 
	 * @return
	 */
	public ArrayList<Poem> getPoems() {
		return poems;
	}
}
